import dayjs from 'dayjs';

export const getBase64 = (file) =>
  new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });

export function formatDate(date: string) {
    return dayjs(date).format('YYYY-MM-DD HH:mm:ss')
}

function downloadFile(url: string, fileName: string) {
  const link = document.createElement('a');
  link.href = url;
  link.download = fileName || 'downloaded-file'; // 设置下载的文件名
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
}

export function batchDownload(files) {
  files.forEach(file => {
    downloadFile(file.url, file.name)

    // fetch(file.url)
    //   .then(response => response.blob())
    //   .then(blob => {
    //       const a = document.createElement('a');
    //       a.href = URL.createObjectURL(blob);
    //       a.download = file.name; // 设置下载文件名
    //       document.body.appendChild(a);
    //       a.click();
    //       URL.revokeObjectURL(a.href); // 释放内存
    //       document.body.removeChild(a); // 移除元素
    //   })
  });
}